package com.hy.mvc.biz.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DecimalFormat;

/**
 * @author Booker
 * @date 2017/7/3
 */
public class MathUtils {
    public static int half(int num) {
        if (num % 2 == 0) {
            return num >> 1;
        }
        return (num - 1) >> 1;
    }

    public static int ceil(int devide, int devider) {
        if (devide % devider == 0) {
            return devide / devider;
        }
        return devide / devider + 1;
    }

    public static int minus(int a, int b) {
        return a - b;
    }

    public static int mod(int devide, int devider) {
        return devide % devider;
    }

    /**
     * 格式化数据至 百分比
     *
     * @param number
     * @return
     */
    public static String formatNumber(Number number) {
        if (number == null) {
            return "";
        }
        try {
            DecimalFormat df1 = new DecimalFormat("##.00%");
            return df1.format(number);
        } catch (Exception e) {
            return "";
        }
    }

    public static String formatNumberWithoutPercent(Number number) {
        if (number == null) {
            return "";
        }
        try {
            DecimalFormat df1 = new DecimalFormat("##.00");
            return df1.format(number);
        } catch (Exception e) {
            return "";
        }
    }

    public static String dividAndformatNumberPercent(double number1, double number2) {
        try {
            DecimalFormat df1 = new DecimalFormat("##0.0000");
            return df1.format(number1 * 100 / number2);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 字符串转LONG
     *
     * @param value
     * @return
     */
    public static Long stringToLong(String value) {
        if (StringUtils.isBlank(value)) {
            return 0L;
        } else {
            try {
                return new Long(value);
            } catch (Exception e) {
                return 0L;
            }
        }
    }

    /**
     * Long类型处理
     *
     * @param value
     * @return
     */
    public static long toLong(Long value) {
        if (null == value) {
            return 0L;
        }
        return value;
    }

}
